package com.klikmee.sms.core.base.test;

import javax.sql.DataSource;

import org.dbunit.database.DatabaseConnection;
import org.dbunit.database.IDatabaseConnection;
import org.dbunit.dataset.IDataSet;
import org.dbunit.ext.hsqldb.HsqldbDataTypeFactory;
import org.dbunit.ext.mssql.InsertIdentityOperation;
import org.dbunit.util.fileloader.DataFileLoader;
import org.dbunit.util.fileloader.FlatXmlDataFileLoader;
import org.junit.Before;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:test-context.xml" })
public class CommonTestBase {

	@Autowired
	@Qualifier("mysqlDataSource")
	private DataSource	dataSource;

	@Before
	public void init() throws Exception {
		// load data from xml
		IDatabaseConnection connection = new DatabaseConnection( dataSource.getConnection() );
		connection.getConfig().setProperty( "http://www.dbunit.org/properties/datatypeFactory", new HsqldbDataTypeFactory() );

		DataFileLoader loader = new FlatXmlDataFileLoader();
		IDataSet dataSet = loader.load( "/smscore_test_full.xml" );
		InsertIdentityOperation.CLEAN_INSERT.execute( connection, dataSet );
	}

}
